<template>
	<view class="container">
		<button type="primary" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
			一键授权登录
		</button>
	</view>
</template>

<script>
	const initSrc = "https://m.51bmj.cn/"
	import {
		authorize,
		appId as wxLoginAppId,
		login
	} from "@/util/api/wxlogin";
	// import { userInfo } from "../../util/api/user";
	// import { pageUrl, appId } from "@/util/api.js";
	export default {
		data() {
			return {
				sessionKey: '',
				openId: ''
			}
		},
		onLoad() {
			this.init()
		},
		methods: {
			init() {
				let that = this
				uni.login({
					provider: "weixin",
					success: function(loginRes) {
						authorize({
							appId: wxLoginAppId,
							code: loginRes.code,
						}).then((res) => {
							that.openId = res.result.openid
							that.sessionKey = res.result.sessionKey
						});
					},
				});
			},
			getPhoneNumber(e) {
				let that = this
				login({
					appId: wxLoginAppId,
					encryptedData: e.detail.encryptedData,
					iv: e.detail.iv,
					openId: this.openid,
					sessionKey: this.sessionKey,
				}).then((res1) => {
					if (res1.code == 200) {
						uni.showToast({
							title: "登录成功"
						})
						uni.setStorageSync('token', res1.message);
						uni.setStorageSync('openId', that.openId);
						
						// let src = initSrc + "pages/login/index#token=" + res1.message + "&openid=" + that.openId
						// wx.navigateTo({
						//       url: '/pages/index/index?redirect='+encodeURIComponent(src),
						//     })
						
						setTimeout(() => {
							uni.navigateBack({

							})
						}, 1500)
					}
				});
			},
		}
	}
</script>

<style>
	.container {
		margin-top: 300rpx;
		padding: 0 24rpx;
	}
</style>
